package com.my.commonlibrary.net.netroid;

import android.app.ProgressDialog;
import android.content.Context;

import com.duowan.mobile.netroid.DefaultRetryPolicy;
import com.duowan.mobile.netroid.Listener;
import com.duowan.mobile.netroid.NetroidError;
import com.duowan.mobile.netroid.Request;
import com.duowan.mobile.netroid.cache.BitmapImageCache;
import com.duowan.mobile.netroid.cache.DiskCache;
import com.duowan.mobile.netroid.image.NetworkImageView;
import com.duowan.mobile.netroid.request.ImageRequest;
import com.duowan.mobile.netroid.request.JsonObjectRequest;

import org.json.JSONObject;

import java.io.File;
import java.text.RuleBasedCollator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Author : Cooper
 * Time : 2016/11/4  15:39
 * Description : Netroid 二次封装
 */

public class Network {
    private static Network instance;
    private Context mContext;
    private Map<String, String> mHead = new HashMap();

    public Network(Context mContext) {
        this.mContext = mContext;
        initNetwork();
    }

    public static Network getInstance(Context context) {
        if (instance == null) {
            instance = new Network(context);
        }
        return instance;
    }

    private void initNetwork() {
        int memoryCacheSize = 5 * 1024 * 1024; // 5MB
        File diskCacheDir = new File(mContext.getCacheDir(), "netroid");
        int diskCacheSize = 50 * 1024 * 1024; // 50MB
        Netroid.init(new DiskCache(diskCacheDir, diskCacheSize));

        Netroid.setImageLoader(new SelfImageLoader(Netroid.getRequestQueue(),
                new BitmapImageCache(memoryCacheSize), mContext.getResources(), mContext.getAssets()) {
            @Override
            public void makeRequest(ImageRequest request) {
                request.setCacheExpireTime(TimeUnit.MINUTES, 1);
            }
        });
    }

    public void globalHead(Map<String, String> head) {
        this.mHead.putAll(head);
    }

    public void get(String url, Listener listener) {
        this.get(url, listener, this.mHead, false, "cyhz_tag");
    }

    public void get(String url, Listener listener, boolean cache) {
        this.get(url, listener, this.mHead, cache, "cyhz_tag");
    }

    public void get(String url, Listener listener, boolean cache, String tag) {
        this.get(url, listener, this.mHead, cache, tag);
    }

    public void get(String url, Listener listener, Map<String, String> head, boolean cache, String tag) {
        JsonObjectRequest jsonRequest = new JsonObjectRequest(0, url, null, listener);
        jsonRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 1, 1.0F));
        Iterator iterator = head.entrySet().iterator();

        while(iterator.hasNext()) {
            Map.Entry entry = (Map.Entry)iterator.next();
            jsonRequest.addHeader((String)entry.getKey(), (String)entry.getValue());
        }

        if(cache) {
            jsonRequest.setCacheExpireTime(TimeUnit.DAYS, 1);
        } else {
            jsonRequest.setForceUpdate(true);
        }

        jsonRequest.setTag(tag);
        Netroid.add(jsonRequest);
    }

    public void post(String url, Map<String, String> params, Listener listener) {
        this.post(url, params, listener, this.mHead, false, "cyhz_tag");
    }

    public void post(String url, Map<String, String> params, Listener listener, boolean cache) {
        this.post(url, params, listener, this.mHead, cache, "cyhz_tag");
    }

    public void post(String url, Map<String, String> params, Listener listener, boolean cache, String tag) {
        this.post(url, params, listener, this.mHead, cache, tag);
    }

    public void post(String url, Map<String, String> params, Listener listener, Map<String, String> head, boolean cache, String tag) {
        NormalPostRequest jsonRequest = new NormalPostRequest(url, params, listener);
        jsonRequest.setRetryPolicy(new DefaultRetryPolicy(100000, 1, 1.0F));
        Iterator iterator = head.entrySet().iterator();

        while(iterator.hasNext()) {
            Map.Entry entry = (Map.Entry)iterator.next();
            jsonRequest.addHeader((String)entry.getKey(), (String)entry.getValue());
        }

        if(cache) {
            jsonRequest.setCacheExpireTime(TimeUnit.DAYS, 1);
        } else {
            jsonRequest.setForceUpdate(true);
        }

        jsonRequest.setTag(tag);
        Netroid.add(jsonRequest);
    }

    public static void displayImage(String url, NetworkImageView imageView, int defaultImageResId, int errorImageResId) {
        Netroid.displayImage(url,imageView,defaultImageResId,errorImageResId);
    }

    private class BaseListener<T> extends Listener<T> {
        private ProgressDialog mProgressDialog;
        private Context context;

        public BaseListener(Context context) {
            this.context = context;
        }

        @Override
        public void onPreExecute() {
            mProgressDialog = ProgressDialog.show(context, null, "request executing");
        }

        @Override
        public void onFinish() {
            if (mProgressDialog != null) {
                mProgressDialog.cancel();
            }
        }

        @Override
        public void onSuccess(T response) {
        }

        @Override
        public void onError(NetroidError error) {
        }
    }
}
